package com.ruoyi.functiontest.service.impl;

import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.functiontest.mapper.InterchangeMileageStatTestMapper;
import com.ruoyi.functiontest.domain.InterchangeMileageStatTest;
import com.ruoyi.functiontest.service.IInterchangeMileageStatTestService;
import com.ruoyi.common.utils.StringUtils;

/**
 * 互通里程实测数据测试Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@Service
public class InterchangeMileageStatTestServiceImpl implements IInterchangeMileageStatTestService 
{
    @Autowired
    private InterchangeMileageStatTestMapper interchangeMileageStatTestMapper;

    /**
     * 查询互通里程实测数据测试
     * 
     * @param id 互通里程实测数据测试主键
     * @return 互通里程实测数据测试
     */
    @Override
    public InterchangeMileageStatTest selectInterchangeMileageStatTestById(Long id)
    {
        return interchangeMileageStatTestMapper.selectInterchangeMileageStatTestById(id);
    }

    /**
     * 查询互通里程实测数据测试列表
     * 
     * @param interchangeMileageStatTest 互通里程实测数据测试
     * @return 互通里程实测数据测试
     */
    @Override
    public List<InterchangeMileageStatTest> selectInterchangeMileageStatTestList(InterchangeMileageStatTest interchangeMileageStatTest)
    {
        return interchangeMileageStatTestMapper.selectInterchangeMileageStatTestList(interchangeMileageStatTest);
    }

    /**
     * 新增互通里程实测数据测试
     * 
     * @param interchangeMileageStatTest 互通里程实测数据测试
     * @return 结果
     */
    @Override
    public int insertInterchangeMileageStatTest(InterchangeMileageStatTest interchangeMileageStatTest)
    {
        return interchangeMileageStatTestMapper.insertInterchangeMileageStatTest(interchangeMileageStatTest);
    }

    /**
     * 修改互通里程实测数据测试
     * 
     * @param interchangeMileageStatTest 互通里程实测数据测试
     * @return 结果
     */
    @Override
    public int updateInterchangeMileageStatTest(InterchangeMileageStatTest interchangeMileageStatTest)
    {
        return interchangeMileageStatTestMapper.updateInterchangeMileageStatTest(interchangeMileageStatTest);
    }

    /**
     * 批量删除互通里程实测数据测试
     * 
     * @param ids 需要删除的互通里程实测数据测试主键
     * @return 结果
     */
    @Override
    public int deleteInterchangeMileageStatTestByIds(Long[] ids)
    {
        return interchangeMileageStatTestMapper.deleteInterchangeMileageStatTestByIds(ids);
    }

    /**
     * 删除互通里程实测数据测试信息
     * 
     * @param id 互通里程实测数据测试主键
     * @return 结果
     */
    @Override
    public int deleteInterchangeMileageStatTestById(Long id)
    {
        return interchangeMileageStatTestMapper.deleteInterchangeMileageStatTestById(id);
    }

    @Override
    public boolean hasChildById(Long id)
    {
        return interchangeMileageStatTestMapper.hasChildById(id) > 0;
    }

    @Override
    public int updateReview1ToZero(Long[] ids)
    {
        return interchangeMileageStatTestMapper.updateReview1ToZero(ids);
    }

    @Override
    public int updateReview2ToZeroByCondition(Long[] ids)
    {
        return interchangeMileageStatTestMapper.updateReview2ToZeroByCondition(ids);
    }

    @Override
    public int updateReview3ToZero(Long[] ids)
    {
        return interchangeMileageStatTestMapper.updateReview3ToZero(ids);
    }

    @Override
    public boolean checkAllReview1IsZero(Long[] ids)
    {
        return interchangeMileageStatTestMapper.checkAllReview1IsZero(ids) == 0;
    }

    @Override
    public boolean checkAllReview1AndReview2AreZero(Long[] ids)
    {
        return interchangeMileageStatTestMapper.checkAllReview1AndReview2AreZero(ids) == 0;
    }

    @Override
    public String importInterchangeMileageStatTest(List<InterchangeMileageStatTest> interchangeMileageStatTestList, boolean isUpdateSupport)
    {
        if (StringUtils.isNull(interchangeMileageStatTestList) || interchangeMileageStatTestList.isEmpty())
        {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        
        for (InterchangeMileageStatTest interchangeMileageStatTest : interchangeMileageStatTestList)
        {
            try
            {
                // 验证基本信息
                if (StringUtils.isNull(interchangeMileageStatTest.getRoadName()))
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、路段名称不能为空");
                    continue;
                }
                
                // 检查是否存在相同id和parentId的数据
                InterchangeMileageStatTest existingNode = interchangeMileageStatTestMapper.selectInterchangeMileageStatTestByIdAndParentId(interchangeMileageStatTest.getId(), interchangeMileageStatTest.getParentId());
                
                if (existingNode != null)
                {
                    if (isUpdateSupport)
                    {
                        // 更新现有数据
                        interchangeMileageStatTest.setId(existingNode.getId());
                        interchangeMileageStatTestMapper.updateInterchangeMileageStatTest(interchangeMileageStatTest);
                        successNum++;
                    }
                    else
                    {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、数据已存在");
                    }
                }
                else
                {
                    // 插入新数据
                    interchangeMileageStatTestMapper.insertInterchangeMileageStatTest(interchangeMileageStatTest);
                    successNum++;
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new RuntimeException(failureMsg.toString());
        }
        else
        {
            successMsg.append("恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }
}
